import { _decorator, Component, instantiate, Label, Node, Prefab, UITransform, UITransformComponent, view } from 'cc';
import { RootController } from './RootController';
const { ccclass, property } = _decorator;

@ccclass('playerController')
export class playerController extends Component {
    @property speed:number = 150
    @property(Prefab) par:Prefab
    //是否可以移动
    IsMove:boolean = false;
    score = 0;
    start() {

    }
    die()
    {
        const par = instantiate(this.par)
        this.node.addChild(par)
        par.on(Node.EventType.NODE_DESTROYED,()=>
        {
            this.node.parent.getChildByName('UiRoot').getChildByName('rest').active = true;
        },this)
    }
    move(deltaTime:number)
    {
        if(this.IsMove)
        {
            const chi = this.node.parent.getChildByName("bridgesRoot").children
            const destX = chi[chi.length - 1].getComponent(UITransform).contentSize.height + chi[chi.length - 1].getPosition().x;
            const x = this.node.getPosition().x;
            const y = this.node.getPosition().y
            this.node.setPosition(x + this.speed * deltaTime,y);
            if(x >= destX)
            {
                this.IsMove = false;
                //获取桥梁子节点
                const bridgeChi = this.node.parent.getChildByName("bridgesRoot").children
                //获取最后一根桥梁的高度
                const height = bridgeChi[bridgeChi.length - 1].getComponent(UITransform).contentSize.height - 5
                //获取桥墩子节点
                const pierChi = this.node.parent.getChildByName("piersRoot").children
                //获取前一根桥墩宽度
                const Pierwidth = pierChi[pierChi.length - 2].getComponent(UITransform).contentSize.width
                //获取最后一根桥墩的宽度
                const lastPierwidth = pierChi[pierChi.length - 1].getComponent(UITransform).contentSize.width
                //获取两桥墩之间间隙
                const gap = pierChi[pierChi.length - 1].getPosition().x - pierChi[pierChi.length - 2].getPosition().x - Pierwidth;
                if(height < gap || height > gap + lastPierwidth)
                {
                    console.log('游戏结束')
                    console.log(`${height}高度`)
                    console.log(`${gap}间隙`)
                    this.node.emit('die')
                }
                else
                {
                    console.log('游戏结束')
                    console.log(`${height}高度`)
                    console.log(`${gap}间隙`)
                    this.node.emit('survival')
                    this.score++;
                    const score = this.node.parent.getChildByName('UiRoot').getChildByName('score')
                    score.getComponent(Label).string = '分数:' + this.score;
                    if(this.score === 5)
                    {
                        navigator.vibrate(5000);
                        const root = this.node.parent.getComponent(RootController)
                        root.isMove = false;
                        const terror = this.node.parent.getChildByName('terror')
                        terror.active = true;
                        const size = view.getFrameSize()
                        terror.getComponent(UITransformComponent).setContentSize(size.width,size.height)
                        setTimeout(()=>
                        {
                            terror.active = false;
                            root.isMove = true;
                        },5000)
                    }
                }
            }
        }
    }
    update(deltaTime: number) 
    {
        this.move(deltaTime);
    }
}


